package com.lotusfall.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCTest06 {
    public static void main(String[] args) {
        Connection connection = null;
        Statement stmt = null;
        try {
            // 1、注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 2、获取链接
            String url = "jdbc:mysql://127.0.0.1:3306/learn?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false";
            String user = "root";
            String password = "994695";
            connection = DriverManager.getConnection(url,user,password);

            // 3、获取数据库操作对象
            stmt = connection.createStatement();

            // 4、执行sql语句
            String sql = "insert dept(deptno,dname,loc) values(20,'人事部','北京');";
            int count = stmt.executeUpdate(sql);
            System.out.println(count);

            // 5、数据集处理


        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }finally {
            // 6、释放资源
            if(stmt != null){
                try{
                    stmt.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
            if(connection != null){
                try{
                    connection.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }
            }
        }

    }
}